<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
import { useBookStore } from '../stores/bookStore';

const store = useBookStore();
const voices = ref<SpeechSynthesisVoice[]>([]);

onMounted(() => {
  // 获取可用的语音列表
  const loadVoices = () => {
    voices.value = store.getAvailableVoices();
    // 如果没有语音，等待语音加载完成
    if (voices.value.length === 0) {
      setTimeout(loadVoices, 100);
    }
  };
  loadVoices();
  
  // 监听语音列表变化
  speechSynthesis.onvoiceschanged = loadVoices;
});

// 中文语音优先
const chineseVoices = computed(() => 
  voices.value.filter(v => v.lang.includes('zh') || v.lang.includes('cmn'))
);

const otherVoices = computed(() => 
  voices.value.filter(v => !v.lang.includes('zh') && !v.lang.includes('cmn'))
);

const handleVoiceChange = (event: Event) => {
  const select = event.target as HTMLSelectElement;
  if (select.value) {
    const voice = voices.value.find(v => v.name === select.value);
    if (voice) {
      store.setVoice(voice);
    }
  } else {
    // 如果选择的是默认语音，设置为null
    store.setVoice(null);
  }
};

const handleRateChange = (event: Event) => {
  const input = event.target as HTMLInputElement;
  store.setRate(parseFloat(input.value));
};

const handlePitchChange = (event: Event) => {
  const input = event.target as HTMLInputElement;
  store.setPitch(parseFloat(input.value));
};

const handleVolumeChange = (event: Event) => {
  const input = event.target as HTMLInputElement;
  store.setVolume(parseFloat(input.value));
};

const handleDelayChange = (event: Event) => {
  const input = event.target as HTMLInputElement;
  store.setAutoPlayDelay(parseInt(input.value));
};

const clearData = () => {
  if (confirm('确定要清除所有数据吗？这将删除保存的书籍、进度和设置。')) {
    store.clearLocalStorage();
    // 重置store状态
    store.$reset();
    // 刷新页面
    window.location.reload();
  }
};
</script>

<template>
  <div class="voice-settings">
    <h3>🎤 语音设置</h3>
    
    <div class="setting-group">
      <label>人声选择:</label>
      <select @change="handleVoiceChange" class="voice-select">
        <option value="">默认语音</option>
        <optgroup label="中文语音">
          <option 
            v-for="voice in chineseVoices" 
            :key="voice.name"
            :value="voice.name"
            :selected="store.voiceSettings.voice?.name === voice.name"
          >
            {{ voice.name }} ({{ voice.lang }})
          </option>
        </optgroup>
        <optgroup label="其他语音">
          <option 
            v-for="voice in otherVoices" 
            :key="voice.name"
            :value="voice.name"
            :selected="store.voiceSettings.voice?.name === voice.name"
          >
            {{ voice.name }} ({{ voice.lang }})
          </option>
        </optgroup>
      </select>
    </div>
    
    <div class="setting-group">
      <label>语速: {{ store.voiceSettings.rate.toFixed(1) }}x</label>
      <input 
        type="range" 
        min="0.1" 
        max="3" 
        step="0.1" 
        :value="store.voiceSettings.rate"
        @input="handleRateChange"
        class="slider"
      />
    </div>
    
    <div class="setting-group">
      <label>音调: {{ store.voiceSettings.pitch.toFixed(1) }}</label>
      <input 
        type="range" 
        min="0.5" 
        max="1.5" 
        step="0.1" 
        :value="store.voiceSettings.pitch"
        @input="handlePitchChange"
        class="slider"
      />
    </div>
    
    <div class="setting-group">
      <label>音量: {{ Math.round(store.voiceSettings.volume * 100) }}%</label>
      <input 
        type="range" 
        min="0" 
        max="1" 
        step="0.1" 
        :value="store.voiceSettings.volume"
        @input="handleVolumeChange"
        class="slider"
      />
    </div>
    
    <div class="setting-group">
      <label>章节间延迟: {{ store.autoPlayDelay }}ms</label>
      <input 
        type="range" 
        min="0" 
        max="5000" 
        step="100" 
        :value="store.autoPlayDelay"
        @input="handleDelayChange"
        class="slider"
      />
      <small class="hint">设置自动播放时章节间的停顿时间</small>
    </div>
    
    <div class="current-voice">
      <strong>当前语音:</strong> 
      {{ store.voiceSettings.voice?.name || '默认语音' }}
    </div>
    
    <div class="actions">
      <button @click="clearData" class="clear-btn">
        🗑️ 清除所有数据
      </button>
      <small class="hint">清除后将需要重新上传书籍</small>
    </div>
  </div>
</template>

<style scoped>
.voice-settings {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 25px;
  padding: 25px;
  box-shadow: 0 8px 32px rgba(255, 245, 157, 0.15);
  border: 1px solid rgba(255, 245, 157, 0.2);
  margin-top: 20px;
  transition: all 0.3s ease;
}

.voice-settings:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 40px rgba(255, 245, 157, 0.2);
}

h3 {
  color: #8d6e63;
  margin-bottom: 20px;
  text-align: center;
  font-size: 1.3rem;
  font-weight: 600;
}

.setting-group {
  margin-bottom: 20px;
}

.setting-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 600;
}

.voice-select {
  width: 100%;
  padding: 10px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 0.9rem;
  background: white;
  transition: border-color 0.3s;
}

.voice-select:focus {
  outline: none;
  border-color: #fdd835;
  box-shadow: 0 0 0 3px rgba(255, 245, 157, 0.1);
}

.slider {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: #e0e0e0;
  outline: none;
  -webkit-appearance: none;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: linear-gradient(135deg, #fdd835, #fff59d);
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 2px 8px rgba(253, 216, 53, 0.3);
}

.slider::-webkit-slider-thumb:hover {
  background: linear-gradient(135deg, #fff59d, #fff176);
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(253, 216, 53, 0.4);
}

.current-voice {
  text-align: center;
  padding: 20px;
  background: rgba(255, 253, 231, 0.6);
  border-radius: 15px;
  color: #8d6e63;
  font-size: 0.9rem;
  border: 1px solid rgba(255, 245, 157, 0.1);
}

.hint {
  display: block;
  margin-top: 5px;
  color: #9e9e9e;
  font-size: 0.8rem;
  font-style: italic;
}

.actions {
  margin-top: 20px;
  text-align: center;
}

.clear-btn {
  background: #ffebee;
  color: #d32f2f;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.3s;
}

.clear-btn:hover {
  background: #ffcdd2;
  transform: translateY(-1px);
}
</style>
